﻿using System;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Data.SqlClient;

namespace MSSQLDAL
{

    //����ʾ�����£� 
    //SqlProcedure.RunProc("p_am_deleteFile", "@a,@b,@c", new object[]{LoginId, Request.UserHostAddress, fileId}); 

    public partial class SqlProcedure
    {

        public SqlProcedure()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ParamName"></param>
        /// <param name="Parms"></param>
        /// <returns></returns>
        public static SqlParameter[] PrepareParameter(string ParamName, object[] Parms)
        {
            int ParamCount;

            string[] paramInfo = ParamName.Split(',');

            ParamCount = Parms.Length < paramInfo.Length ? Parms.Length : paramInfo.Length;

            SqlParameter[] _param = new SqlParameter[ParamCount + 1];

            _param[0] = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
            _param[0].Direction = ParameterDirection.ReturnValue;
            _param[0].Value = 0;
            for (int i = 0; i < ParamCount; i++)
            {
                _param[i + 1] = new SqlParameter(paramInfo[i], Parms[i]);
            }

            return _param;
        }

        /// <summary>
        /// ִ����ݿ�洢��
        /// </summary>
        /// <param name="ProcName">̴洢����</param>
        /// <param name="ParamName">��������ַ��","Էָ</param>
        /// <param name="Parms">�������</param>
        /// <param name="retValue">鷵��ֵ�����ô��ݣ�</param>
        /// <returns>�洢���ִ�е���ݼ�</returns>
        public static DataSet RunProc(string ProcName, string ParamName, object[] Parms, ref int retValue)
        {
            DataSet ds;

            SqlParameter[] _param;
            _param = PrepareParameter(ParamName, Parms);
            ds = SqlHelper.ExecuteDataset(Conn.SqlConnStr, CommandType.StoredProcedure, ProcName, _param);
            retValue = (int)_param[0].Value;

            return ds;

        }

        /// <summary>
        /// ִ����ݿ�洢��
        /// </summary>
        /// <param name="ProcName">̴洢����</param>
        /// <param name="ParamName">��������ַ��","Էָ</param>
        /// <param name="Parms">�������</param>
        /// <returns>�ִ�еķ���ֵ</returns>
        public static int RunProc(string ProcName, string ParamName, object[] Parms)
        {
            SqlParameter[] _param;
            _param = PrepareParameter(ParamName, Parms);
            SqlHelper.ExecuteScalar(Conn.SqlConnStr, CommandType.StoredProcedure, ProcName, _param);
            return (int)_param[0].Value;

        }


        /// ִ����ݿ�洢��
        /// </summary>
        /// <param name="lyTans">�������</param>
        /// <param name="ProcName">�洢����</param>
        /// <param name="ParamName">��������ַ��","Էָ</param>
        /// <param name="Parms">�������</param>
        /// <param name="retValue">鷵��ֵ�����ô��ݣ�</param>
        /// <returns>�洢���ִ�е���ݼ�</returns>
        public static DataSet RunProc(SqlTransaction Tans, string ProcName, string ParamName, object[] Parms, ref int retValue)
        {
            DataSet ds;

            SqlParameter[] _param;
            _param = PrepareParameter(ParamName, Parms);
            ds = SqlHelper.ExecuteDataset(Tans.Connection, CommandType.StoredProcedure, ProcName, _param);
            retValue = (int)_param[0].Value;

            return ds;

        }

        /// <summary>
        /// ִ����ݿ�洢��
        /// </summary>
        /// <param name="ProcName">�������</param>
        /// <param name="ProcName">�洢����</param>
        /// <param name="ParamName">��������ַ��","Էָ</param>
        /// <param name="Parms">�������</param>
        /// <returns>�ִ�еķ���ֵ</returns>
        public static int RunProc(SqlTransaction Tans, string ProcName, string ParamName, object[] Parms)
        {
            SqlParameter[] _param;
            _param = PrepareParameter(ParamName, Parms);
            SqlHelper.ExecuteScalar(Tans.Connection, CommandType.StoredProcedure, ProcName, _param);
            return (int)_param[0].Value;
        }
    }
}